/**
 * 导出审计
 */
var Push = function () {

    return {

        /*绑定轨迹导出*/
        subPush: function () {

                var type = $('#type option:selected').val();
                var remoteUrl = $("#basePath").val()+type;
                $.ajax({
                    type: "post",
                    url: remoteUrl,
                    data: {
                        receiver:$("#receiver").val(),
                        title: $("#title").val(),
                        content: $("#content").val(),
                        type: type
                    },
                    dataType: "json",
                    success: function (data) {
                        if (data.returnCode == 0) {
                            if(type == "approve"){
                                //如果是审批则需要继续调用其他方法
                                $("#messageTip").html("提交审批成功，请等待管理员审批...");
                                //获取二维码状态
                                $("#subPushButton").attr({"disabled":"disabled"});
                                var code = data.tempData;
                                Push.qrStatus(code);

                            }else{
                                $("#messageTip").html("推送成功");
                            }

                        } else {
                            $("#messageTip").html(data.returnMessage);
                        }
                    }
                });

        },
        qrStatus: function (code) {

            var timeOut = 0;
            var remoteUrl = $("#basePath").val()+"status";
            var wait = setInterval(function() {
                $.ajax({
                    type : "post",
                    url : remoteUrl,
                    dataType : "json",
                    data: {
                        code: code
                    },
                    async : true,
                    success : function(data) {
                        if (data.returnCode == 0) {
                            clearInterval(wait);
                            Push.approveBack(data.data);
                        }else {

                        }
                    },
                    error : function() {
                        clearInterval(wait);
                        return;
                    }
                });
                if(timeOut == 60){
                    clearInterval(wait);

                }
                timeOut += 1;
            }, 1000);
        },

        approveBack: function (url) {
                $.ajax({
                    type : "post",
                    url : url,
                    dataType : "json",
                    async : true,
                    success : function(data) {
                        if (data.returnCode == 0) {
                            var innerH = "管理员"+(data.result=="1"?"同意":"拒绝")+"你的审批请求";
                            $("#messageTip").html(innerH);
                        }else if(data.returnCode == -1){
                            $("#messageTip").html("审批失败");
                        }else {
                            $("#messageTip").html("审批失败");
                        }
                    },
                    error : function() {
                        $("#messageTip").html("审批失败");
                        return;
                    }
                });


        }

    };

}();